import axios from 'axios'
// import { MessageBox, Message } from 'element-ui'
import { ElMessage  } from 'element-plus'
// import { showScreenLoading, hideScreenLoading } from "./loading";
// import store from '@/store'
// import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API,
  withCredentials: true, // send cookies when cross-domain requests
  // timeout: 50000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    if (response?.data?.Data?.Error) {
      ElMessage ({
        message: response?.data?.Data?.Error?.Msg || '发生错误',
        type: 'error',
        duration: 5 * 1000
      })
      return false
    }
    if (response?.data?.Status?.StatusCode === 2000) {
      ElMessage ({
        message: '登录超时，请重新登录',
        type: 'error',
        duration: 5 * 1000
      })
      sessionStorage.clear();
      this.router.push('/')
    }

    const res = response.data
    return res
  },
  error => {
    // hideScreenLoading();
    console.log('err' + error) // for debug
    // ElMessage({
    //   message: error.message === 'timeout of 20000ms exceeded' ? '请求超时' : error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    return Promise.reject(error)
  }
)

export default service
